import type { AppTreeData } from '@/components/AppTree/types'

const useTree = () => {
  const openedLeave = ref<Set<string>>(new Set())

  const useOpenLeave = (leave: AppTreeData) => {
    if (!openedLeave.value.has(leave.value)) {
      openedLeave.value.add(leave.value)
      return leave
    }
    openedLeave.value.delete(leave.value)
  }

  const useLeaveHasOpened = (value: string): boolean => {
    return openedLeave.value.has(value)
  }

  const selectedList = ref()

  let multiple: boolean

  const initSelectedList = (multipleVal: boolean) => {
    multiple = multipleVal
    if (multiple) {
      selectedList.value = []
      return
    }
    selectedList.value = ''
  }

  const useSelectLeave = (leave: AppTreeData) => {
    if (multiple) {
      selectedList.value.push(leave)
      return leave
    }
    selectedList.value = leave.value
    return leave
  }

  return {
    useOpenLeave,
    useLeaveHasOpened,
    selectedList,
    initSelectedList,
    useSelectLeave
  }
}

export default useTree
